package dfbz.com.controller.view;


import dfbz.com.controller.BaseServlet;
import dfbz.com.model.Article;
import dfbz.com.service.ArticleService;
import dfbz.com.service.StudentService;
import dfbz.com.service.UserInfoService;
import dfbz.com.service.impl.ArticleServicelmpl;
import dfbz.com.service.impl.StudentServiceImpl;
import dfbz.com.service.impl.UserInfoServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/view/*")
public class UIServlet extends BaseServlet {

    private StudentService studentService = new StudentServiceImpl();
    private UserInfoService userInfoService = new UserInfoServiceImpl();
    private  ArticleService articleService=new ArticleServicelmpl();



    public void myView(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //每次刷新进来渲染之前，就去获取最新的学生列表
        req.getSession().setAttribute("stuList", studentService.findAll());
        //resp.setCharacterEncoding("UTF-8");
        String msg;
        Object obj = req.getSession().getAttribute("msg");
        if (null != obj) {
            msg = obj.toString();
            if ("error".equals(msg)) {
                req.getRequestDispatcher("/loginfail.jsp").forward(req, resp);
            } else if ("info".equals(msg)) {
                req.getRequestDispatcher("/info.jsp").forward(req, resp);
            } else if ("update".equals(msg)) {
                req.getRequestDispatcher("/update.jsp").forward(req, resp);
            }
        } else {
            req.getRequestDispatcher("/info.jsp").forward(req, resp);
        }
    }

    public void userlist(HttpServletRequest req, HttpServletResponse resp) {
//        String userId = req.getAttribute("userId").toString();
        String page1 = req.getParameter("page");
        int page = 1;
//         int pages = Integer.parseInt(page1);
        if (page1 != null) {
            page = Integer.parseInt(page1);
        }


        req.setAttribute("userinfoList", userInfoService.findList(null, page));
        int count = userInfoService.findUserInfo().size()/5;

        req.setAttribute("usertotal", count+1  );


        try {
            req.getRequestDispatcher("/html/user.jsp").forward(req, resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void home(HttpServletRequest req, HttpServletResponse resp) {
        try {
            req.getRequestDispatcher("/html/home.jsp").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void myuser(HttpServletRequest req, HttpServletResponse resp) {
        try {
            req.getRequestDispatcher("/html/my_user.jsp").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void article(HttpServletRequest req, HttpServletResponse resp) {
        String page1 = req.getParameter("page");
        int page = 1;
//         int pages = Integer.parseInt(page1);
        if (page1 != null) {
            page = Integer.parseInt(page1);
        }

        int count=articleService.findArticle(page).size()/5;
        req.getSession().setAttribute("articlecount",count+1);
       req.getSession().setAttribute("ArtcleList",articleService.findArticle(page));
        try {
            req.getRequestDispatcher("/html/article.jsp").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void articledetail(HttpServletRequest req, HttpServletResponse resp) {
        try {
            req.getRequestDispatcher("/html/article_detail.jsp").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
//    public void total(HttpServletRequest req, HttpServletResponse resp){
//        req.setAttribute("usertotalList", userInfoService.findUserInfo());
//
//    }
}
